package com.mq;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

import java.sql.SQLOutput;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Procer2_12 {
    public static void main(String[] args) throws Exception {
//        DefaultMQProducer producer = new DefaultMQProducer("producer_group", true);
        TransactionMQProducer producer = new TransactionMQProducer("producer_group");
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.setTransactionListener(new TransactionListener() {
            @Override
            public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
                System.out.println("half后回调本地事务"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
                return LocalTransactionState.UNKNOW;
            }

            @Override
            public LocalTransactionState checkLocalTransaction(MessageExt msg) {
                System.out.println("本地事务无响应后，执行检查操作"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
                return LocalTransactionState.COMMIT_MESSAGE;
            }
        });
        producer.start();

        List<Order> orders1 = buileOrderList(1, "A", "Tag_1");
        List<Order> orders2 = buileOrderList(1, "B", "Tag_2");
        List<Order> orders3 = buileOrderList(1, "C", "Tag_3");
        List<Order> orders4 = buileOrderList(2, "A", "Tag_1");
        List<Order> orders5 = buileOrderList(2, "B", "Tag_2");
        List<Order> orders6 = buileOrderList(2, "C", "Tag_3");

        List<Order> ordersn = new ArrayList<>();
        ordersn.addAll(orders1);
        ordersn.addAll(orders2);
        ordersn.addAll(orders3);
        ordersn.addAll(orders4);
        ordersn.addAll(orders5);
        ordersn.addAll(orders6);

        List<Message> messages = new ArrayList<>();
        for (int i = 0; i < ordersn.size(); i++) {
            Message msg = new Message(
                    "TAG_topic",
                    ordersn.get(i).tag,
                    ("( " + i + " ) " + ordersn.get(i).toString() + "").getBytes());
            msg.setKeys("tag_key");
            msg.putUserProperty("idx",i+"");
            messages.add(msg);
            producer.sendMessageInTransaction(msg,null);
        }
        System.out.println("Send Finished.===>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
    }

    public static List<Order> buileOrderList(int id, String desc, String tag) {
        List<Order> arrayList = new ArrayList<>();
        Order order = new Order();
        order.orderId = id;
        order.desc = desc;
        order.tag = tag;
        arrayList.add(order);
        return arrayList;
    }


    public static class Order {
        int orderId;
        String desc;
        String tag;

        @Override
        public String toString() {
            return "Order{" +
                    "orderId=" + orderId +
                    ", desc='" + desc + '\'' +
                    ", tag='" + tag + '\'' +
                    '}';
        }
    }
}
